<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="以英文,号分割可同时新增多个部门"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createdTime" hidden>
      <el-input v-model="dataForm.createdTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除 0:未删除 1:已删除" prop="isDelete" hidden>
      <el-input v-model="dataForm.isDelete" placeholder="是否删除 0:未删除 1:已删除"></el-input>
    </el-form-item>
    <el-form-item label="业务关联" prop="businessDept">
      <el-select v-model="dataForm.businessId" value-key="value" placeholder="部门关联业务工作"  value="">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
          :disabled="item.disabled">
        </el-option>
      </el-select>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="isLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        isLoading: false,
        dataForm: {
          id: 0,
          name: '',
          createdTime: '',
          isDelete: '',
          businessId: ''
        },
        options: [{
          value: '0',
          label: '农业产出'
        }, {
          value: '1',
          label: '生产能力'
        }, {
          value: '2',
          label: '绿色发展'
        }, {
          value: '3',
          label: '乡村建设'
        }, {
          value: '4',
          label: '产业发展'
        }, {
          value: '5',
          label: '农村改革'
        }],
        dataRule: {
          name: [
            { required: false, message: '部门名称不能为空', trigger: 'blur' }
          ],
          businessId: [
            { required: false, message: '业务工作关联不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.isLoading = false
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/businessdept/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.businessDept.name
                this.dataForm.createdTime = data.businessDept.createdTime
                this.dataForm.isDelete = data.businessDept.isDelete
                this.dataForm.businessId = data.businessDept.businessId.toString()
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isLoading = true
            this.$http({
              url: this.$http.adornUrl(`/business/businessdept/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'createdTime': this.dataForm.createdTime,
                'isDelete': this.dataForm.isDelete,
                'businessId': this.dataForm.businessId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
